﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Rent;

namespace Jungle.PRVI.WebControls
{
    public class AllOffers : WebControl, IPostBackEventHandler
    {
        private int OnPage = 5;
        private int CurrPage = 1;
        //--------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "chP") // перелистывание страницы
            {
                CurrPage = Convert.ToInt32(s2);
            }
        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            int _width = 0;
            int _height = 0;

            Offer offs = new Offer();
            offs.GetAll();
            for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
            {
                if (i >= offs.OfferList.Count) break;
                Offer o = offs.OfferList[i];

                string imgUrl = UrlHelper.GetSiteUrl();
                int topMove = 0;
                if (o.Photos.Length > 10)
                {
                    string ff = "";
                    if (o.Photos.IndexOf(",") != -1)
                    {
                        ff = o.Photos.Substring(0, o.Photos.IndexOf(","));
                        imgUrl += ff;
                    }
                    else
                    {
                        ff = o.Photos;
                        imgUrl += ff;
                    }
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Context.Server.MapPath(ff));


                    _width = 110;
                    if (img.Width < _width)
                        _width = img.Width;

                    double d1 = Math.Round((double)img.Width / _width, 4);
                    double d2 = Math.Round((double)img.Height / d1, 4);
                    _height = Convert.ToInt32(d2);

                    if (_height > 69) topMove = (-1) * (_height - 69) / 2;
                }
                else
                    imgUrl += "/App_Themes/prvi/img/el/nophoto_mini.jpg";

                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/offers.aspx?cmd=show&id=" + o.OfferId.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute("class", "preItem");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute("class", "bg_preImg");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.AddAttribute("class", "preImg");
                        writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, imgUrl);
                        //writer.AddStyleAttribute(HtmlTextWriterStyle.Width, _width.ToString());
                        //writer.AddStyleAttribute(HtmlTextWriterStyle.Height, _height.ToString());
                        writer.AddStyleAttribute("background-position", "0px " + topMove.ToString() + "px");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "pre_descr");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    try { writer.Write(o.Description.Substring(0, 200)); }
                    catch { writer.Write(o.Description); }
                    writer.RenderEndTag();

                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            if (offs.OfferList.Count % OnPage == 0)
                RenderPageLister(writer, offs.OfferList.Count / OnPage);
            else
                RenderPageLister(writer, (offs.OfferList.Count / OnPage) + 1);

            base.RenderContents(writer);
        }

        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
        {
            ClientScriptManager cs = Page.ClientScript;
            writer.Write("<b>Страница: </b>");
            for (int i = 1; i <= count; i++)
            {
                if (i == CurrPage)
                    writer.Write("<b>" + i.ToString() + "</b>");
                else
                {
                    PostBackOptions opt = new PostBackOptions(this);
                    opt.ActionUrl = Context.Request.RawUrl;
                    opt.Argument = "chP," + i.ToString();

                    writer.AddAttribute("onclick", cs.GetPostBackEventReference(opt));
                    writer.AddAttribute("style", "cursor: pointer;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
                if (i < count) writer.Write(", ");
            }
            return writer;
        }
    }
}
